<?php

class SessionBrowser extends TTemplateControl
{	
	public function onLoad()
	{
		$this->reloadAllItems();
		$this->reloadSelectedItems();
	}
	
	public function addButtonClicked($sender, $param)
	{
		$selected_session_list = $this->getViewState('selected_session_list', array());

		foreach($this->all_list->getSelectedIndices() as $selected_index)
		{
			$selected_item = array(	'id' => $this->all_list->DataSource[$selected_index]['id'],
									'name' => $this->all_list->DataSource[$selected_index]['name']);
			if(array_search($selected_item, $selected_session_list) === false)
				$selected_session_list[] = $selected_item;
		}

		$this->setViewState('selected_session_list', $selected_session_list, array());
		$this->reloadSelectedItems();
		$this->clearSelections();
	}
	
	
	public function removeButtonClicked($sender, $param)
	{
		$selected_session_list = $this->getViewState('selected_session_list', array());
		$new_selected_session_list = array();
		
		foreach($selected_session_list as $selected_item)
			if(array_search($selected_item['id'], $this->selected_list->SelectedValues) === false)
				$new_selected_session_list[] = $selected_item;
		
		$this->setViewState('selected_session_list', $new_selected_session_list, array());
		$this->reloadSelectedItems();
		$this->clearSelections();
	}
	
	public function reloadSelectedItems()
	{
		$selected_session_list = $this->getViewState('selected_session_list', array());
		$this->selected_list->DataValueField = 'id';
		$this->selected_list->DataTextField  = 'name';
		$this->selected_list->DataSource = $selected_session_list;
		$this->selected_list->dataBind();
	}
	
	public function reloadAllItems()
	{
		$session_manager = $this->Application->Modules['sessions'];
		$sessions = $session_manager->getSessionsList();
		$session_list = array();
		foreach($sessions as $session)
			$session_list[] = array('id' => $session->getId(), 'name' => $session->getName());
		
		$this->all_list->DataValueField = 'id';
		$this->all_list->DataTextField  = 'name';
		$this->all_list->DataSource = $session_list;
		$this->all_list->dataBind();
	}
	
	public function clearSelections()
	{
		$this->selected_list->SelectedIndices = array();
		$this->all_list->SelectedIndices = array();
	}
	
	public function setSelectedSessionIds($ids)
	{
		$session_manager = $this->Application->Modules['sessions'];
		$selected_session_list = array();
		
		foreach($ids as $id)
		{
			if(($session = $session_manager->getSessionById($id)) instanceof SessionRecord)
				$selected_session_list[] = array(	'id' => $session->getId(),
											 		'name' => $session->getName());
		}
		
		$this->setViewState('selected_session_list', $selected_session_list, array());
	}
	
	public function getSelectedSessionIds()
	{
		$ids = array();
		foreach($this->getViewState('selected_session_list', array()) as $selected_item)
			$ids[] = $selected_item['id'];
		return $ids;
	}
}

?>